home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_perl.idb / usr / freeware / catman / u_man / cat1 / perlfaq7.Z / perlfaq7
Encoding:
Text File  |  1998-10-28  |  44.3 KB  |  1,189 lines

  1.  
  2.  
  3.  
  4.      PPPPEEEERRRRLLLLFFFFAAAAQQQQ7777((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLFFFFAAAAQQQQ7777((((1111))))
  5.  
  6.  
  7.  
  8.      NNNNAAAAMMMMEEEE
  9.       perlfaq7 - Perl Language Issues ($Revision: 1.21 $, $Date:
  10.       1998/06/22 15:20:07 $)
  11.  
  12.      DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  13.       This section deals with general Perl language    issues that
  14.       don't    clearly    fit into any of    the other sections.
  15.  
  16.       CCCCaaaannnn IIII    ggggeeeetttt aaaa BBBBNNNNFFFF////yyyyaaaacccccccc////RRRREEEE ffffoooorrrr tttthhhheeee PPPPeeeerrrrllll llllaaaannnngggguuuuaaaaggggeeee????
  17.  
  18.       There    is no BNF, but you can paw your    way through the    yacc
  19.       grammar in perly.y in    the source distribution    if you're
  20.       particularly brave.  The grammar relies on very smart
  21.       tokenizing code, so be prepared to venture into toke.c as
  22.       well.
  23.  
  24.       In the words of Chaim    Frenkel: "Perl's grammar can not be
  25.       reduced to BNF.  The work of parsing perl is distributed
  26.       between yacc,    the lexer, smoke and mirrors."
  27.  
  28.       WWWWhhhhaaaatttt aaaarrrreeee aaaallllllll tttthhhheeeesssseeee $$$$@@@@%%%%**** ppppuuuunnnnccccttttuuuuaaaattttiiiioooonnnn ssssiiiiggggnnnnssss,,,, aaaannnndddd hhhhoooowwww ddddoooo    IIII kkkknnnnoooowwww
  29.       wwwwhhhheeeennnn ttttoooo uuuusssseeee tttthhhheeeemmmm????
  30.  
  31.       They are type    specifiers, as detailed    in the _p_e_r_l_d_a_t_a
  32.       manpage:
  33.  
  34.           $    for scalar values (number, string or reference)
  35.           @    for arrays
  36.           %    for hashes (associative    arrays)
  37.           *    for all    types of that symbol name.  In version 4 you used them like
  38.         pointers, but in modern    perls you can just use references.
  39.  
  40.       While    there are a few    places where you don't actually    need
  41.       these    type specifiers, you should always use them.
  42.  
  43.       A couple of others that you're likely    to encounter that
  44.       aren't really    type specifiers    are:
  45.  
  46.           <> are used for inputting    a record from a    filehandle.
  47.           \     takes a reference to something.
  48.  
  49.       Note that <FILE> is _n_e_i_t_h_e_r the type specifier for files nor
  50.       the name of the handle.  It is the <>    operator applied to
  51.       the handle FILE.  It reads one line (well, record - see the
  52.       section on $/    in the _p_e_r_l_v_a_r manpage)    from the handle    FILE
  53.       in scalar context, or    _a_l_l lines in list context.  When
  54.       performing open, close, or any other operation besides <> on
  55.       files, or even talking about the handle, do _n_o_t use the
  56.       brackets.  These are correct:    eof(FH), seek(FH, 0, 2)    and
  57.       "copying from    STDIN to FILE".
  58.  
  59.  
  60.  
  61.  
  62.  
  63.      Page 1                        (printed 10/23/98)
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.      PPPPEEEERRRRLLLLFFFFAAAAQQQQ7777((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLFFFFAAAAQQQQ7777((((1111))))
  71.  
  72.  
  73.  
  74.       DDDDoooo IIII aaaallllwwwwaaaayyyyssss////nnnneeeevvvveeeerrrr hhhhaaaavvvveeee ttttoooo qqqquuuuooootttteeee mmmmyyyy ssssttttrrrriiiinnnnggggssss oooorrrr    uuuusssseeee sssseeeemmmmiiiiccccoooolllloooonnnnssss
  75.       aaaannnndddd ccccoooommmmmmmmaaaassss????
  76.  
  77.       Normally, a bareword doesn't need to be quoted, but in most
  78.       cases    probably should    be (and    must be    under use strict).
  79.       But a    hash key consisting of a simple    word (that isn't the
  80.       name of a defined subroutine)    and the    left-hand operand to
  81.       the => operator both count as    though they were quoted:
  82.  
  83.           This              is like this
  84.           ------------          ---------------
  85.           $foo{line}          $foo{"line"}
  86.           bar => stuff          "bar" => stuff
  87.  
  88.       The final semicolon in a block is optional, as is the    final
  89.       comma    in a list.  Good style (see the    _p_e_r_l_s_t_y_l_e manpage)
  90.       says to put them in except for one-liners:
  91.  
  92.           if ($whoops) { exit 1 }
  93.           @nums = (1, 2, 3);
  94.  
  95.           if ($whoops) {
  96.           exit 1;
  97.           }
  98.           @lines = (
  99.           "There Beren came from mountains cold",
  100.           "And lost he wandered    under leaves",
  101.           );
  102.  
  103.  
  104.       HHHHoooowwww ddddoooo IIII sssskkkkiiiipppp    ssssoooommmmeeee rrrreeeettttuuuurrrrnnnn vvvvaaaalllluuuueeeessss????
  105.  
  106.       One way is to    treat the return values    as a list and index
  107.       into it:
  108.  
  109.           $dir = (getpwnam($user))[7];
  110.  
  111.       Another way is to use    undef as an element on the left-hand-
  112.       side:
  113.  
  114.           ($dev, $ino, undef, undef, $uid, $gid) = stat($file);
  115.  
  116.  
  117.       HHHHoooowwww ddddoooo IIII tttteeeemmmmppppoooorrrraaaarrrriiiillllyyyy bbbblllloooocccckkkk wwwwaaaarrrrnnnniiiinnnnggggssss????
  118.  
  119.       The $^W variable (documented in the _p_e_r_l_v_a_r manpage)
  120.       controls runtime warnings for    a block:
  121.  
  122.           {
  123.           local    $^W = 0;    # temporarily turn off warnings
  124.           $a = $b + $c;        # I know these might be    undef
  125.           }
  126.  
  127.  
  128.  
  129.      Page 2                        (printed 10/23/98)
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.      PPPPEEEERRRRLLLLFFFFAAAAQQQQ7777((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLFFFFAAAAQQQQ7777((((1111))))
  137.  
  138.  
  139.  
  140.       Note that like all the punctuation variables,    you cannot
  141.       currently use    _m_y() on    $^W, only _l_o_c_a_l().
  142.  
  143.       A new    use warnings pragma is in the works to provide finer
  144.       control over all this.  The curious should check the perl5-
  145.       porters mailing list archives    for details.
  146.  
  147.       WWWWhhhhaaaatttt''''ssss aaaannnn eeeexxxxtttteeeennnnssssiiiioooonnnn????
  148.  
  149.       A way    of calling compiled C code from    Perl.  Reading the
  150.       _p_e_r_l_x_s_t_u_t manpage is a good place to learn more about
  151.       extensions.
  152.  
  153.       WWWWhhhhyyyy ddddoooo PPPPeeeerrrrllll ooooppppeeeerrrraaaattttoooorrrrssss    hhhhaaaavvvveeee ddddiiiiffffffffeeeerrrreeeennnntttt pppprrrreeeecccceeeeddddeeeennnncccceeee tttthhhhaaaannnn CCCC
  154.       ooooppppeeeerrrraaaattttoooorrrrssss????
  155.  
  156.       Actually, they don't.     All C operators that Perl copies have
  157.       the same precedence in Perl as they do in C.    The problem is
  158.       with operators that C    doesn't    have, especially functions
  159.       that give a list context to everything on their right, eg
  160.       print, chmod,    exec, and so on.  Such functions are called
  161.       "list    operators" and appear as such in the precedence    table
  162.       in the _p_e_r_l_o_p    manpage.
  163.  
  164.       A common mistake is to write:
  165.  
  166.           unlink $file || die "snafu";
  167.  
  168.       This gets interpreted    as:
  169.  
  170.           unlink ($file || die "snafu");
  171.  
  172.       To avoid this    problem, either    put in extra parentheses or
  173.       use the super    low precedence or operator:
  174.  
  175.           (unlink $file) ||    die "snafu";
  176.           unlink $file or die "snafu";
  177.  
  178.       The "English"    operators (and,    or, xor, and not) deliberately
  179.       have precedence lower    than that of list operators for    just
  180.       such situations as the one above.
  181.  
  182.       Another operator with    surprising precedence is
  183.       exponentiation.  It binds more tightly even than unary
  184.       minus, making    -2**2 product a    negative not a positive    four.
  185.       It is    also right-associating,    meaning    that 2**3**2 is    two
  186.       raised to the    ninth power, not eight squared.
  187.  
  188.       Although it has the same precedence as in C, Perl's ?:
  189.       operator produces an lvalue.    This assigns $x    to either $a
  190.       or $b, depending on the trueness of $maybe:
  191.  
  192.  
  193.  
  194.  
  195.      Page 3                        (printed 10/23/98)
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.      PPPPEEEERRRRLLLLFFFFAAAAQQQQ7777((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLFFFFAAAAQQQQ7777((((1111))))
  203.  
  204.  
  205.  
  206.           ($maybe ?    $a : $b) = $x;
  207.  
  208.  
  209.       HHHHoooowwww ddddoooo IIII ddddeeeeccccllllaaaarrrreeee////ccccrrrreeeeaaaatttteeee aaaa ssssttttrrrruuuuccccttttuuuurrrreeee????
  210.  
  211.       In general, you don't    "declare" a structure.    Just use a
  212.       (probably anonymous) hash reference.    See the    _p_e_r_l_r_e_f
  213.       manpage and the _p_e_r_l_d_s_c manpage for details.    Here's an
  214.       example:
  215.  
  216.           $person =    {};              #    new anonymous hash
  217.           $person->{AGE}  =    24;          #    set field AGE to 24
  218.           $person->{NAME} =    "Nat";          #    set field NAME to "Nat"
  219.  
  220.       If you're looking for    something a bit    more rigorous, try the
  221.       _p_e_r_l_t_o_o_t manpage.
  222.  
  223.       HHHHoooowwww ddddoooo IIII ccccrrrreeeeaaaatttteeee aaaa mmmmoooodddduuuulllleeee????
  224.  
  225.       A module is a    package    that lives in a    file of    the same name.
  226.       For example, the Hello::There    module would live in
  227.       Hello/There.pm.  For details,    read the _p_e_r_l_m_o_d manpage.
  228.       You'll also find the _E_x_p_o_r_t_e_r    manpage    helpful.  If you're
  229.       writing a C or mixed-language    module with both C and Perl,
  230.       then you should study    the _p_e_r_l_x_s_t_u_t manpage.
  231.  
  232.       Here's a convenient template you might wish you use when
  233.       starting your    own module.  Make sure to change the names
  234.       appropriately.
  235.  
  236.           package Some::Module;  # assumes Some/Module.pm
  237.  
  238.           use strict;
  239.  
  240.           BEGIN {
  241.           use Exporter     ();
  242.           use vars     qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
  243.  
  244.           ## set the version for version checking; uncomment to    use
  245.           ## $VERSION      = 1.00;
  246.  
  247.           # if using RCS/CVS, this next    line may be preferred,
  248.           # but    beware two-digit versions.
  249.           $VERSION = do{my@r=q$Revision: 1.21 $=~/\d+/g;sprintf    '%d.'.'%02d'x$#r,@r};
  250.  
  251.           @ISA           = qw(Exporter);
  252.           @EXPORT      = qw(&func1 &func2 &func3);
  253.           %EXPORT_TAGS = ( );      # eg:    TAG => [ qw!name1 name2! ],
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.      Page 4                        (printed 10/23/98)
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.      PPPPEEEERRRRLLLLFFFFAAAAQQQQ7777((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLFFFFAAAAQQQQ7777((((1111))))
  269.  
  270.  
  271.  
  272.           # your exported package globals go here,
  273.           # as well as any optionally exported functions
  274.           @EXPORT_OK   = qw($Var1 %Hashit);
  275.           }
  276.           use vars        @EXPORT_OK;
  277.  
  278.           #    non-exported package globals go    here
  279.           use vars        qw(    @more $stuff );
  280.  
  281.           #    initialize package globals, first exported ones
  282.           $Var1   =    '';
  283.           %Hashit =    ();
  284.  
  285.           #    then the others    (which are still accessible as $Some::Module::stuff)
  286.           $stuff  =    '';
  287.           @more   =    ();
  288.  
  289.           #    all file-scoped    lexicals must be created before
  290.           #    the functions below that use them.
  291.  
  292.           #    file-private lexicals go here
  293.           my $priv_var    =    '';
  294.           my %secret_hash =    ();
  295.  
  296.           #    here's a file-private function as a closure,
  297.           #    callable as &$priv_func;  it cannot be prototyped.
  298.           my $priv_func = sub {
  299.           # stuff goes here.
  300.           };
  301.  
  302.           #    make all your functions, whether exported or not;
  303.           #    remember to put    something interesting in the {}    stubs
  304.           sub func1         {}       # no    prototype
  305.           sub func2()    {}       # proto'd void
  306.           sub func3($$)  {}       # proto'd to    2 scalars
  307.  
  308.           #    this one isn't exported, but could be called!
  309.           sub func4(\%)  {}       # proto'd to    1 hash ref
  310.  
  311.           END { }        # module clean-up code here    (global    destructor)
  312.  
  313.           1;        # modules must return true
  314.  
  315.  
  316.       HHHHoooowwww ddddoooo IIII ccccrrrreeeeaaaatttteeee aaaa ccccllllaaaassssssss????
  317.  
  318.       See the _p_e_r_l_t_o_o_t manpage for an introduction to classes and
  319.       objects, as well as the _p_e_r_l_o_b_j manpage and the _p_e_r_l_b_o_t
  320.       manpage.
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.      Page 5                        (printed 10/23/98)
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.      PPPPEEEERRRRLLLLFFFFAAAAQQQQ7777((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLFFFFAAAAQQQQ7777((((1111))))
  335.  
  336.  
  337.  
  338.       HHHHoooowwww ccccaaaannnn IIII tttteeeellllllll iiiiffff aaaa vvvvaaaarrrriiiiaaaabbbblllleeee iiiissss ttttaaaaiiiinnnntttteeeedddd????
  339.  
  340.       See the section on _L_a_u_n_d_e_r_i_n_g    _a_n_d _D_e_t_e_c_t_i_n_g _T_a_i_n_t_e_d _D_a_t_a in
  341.       the _p_e_r_l_s_e_c manpage.    Here's an example (which doesn't use
  342.       any system calls, because the    _k_i_l_l() is given    no processes
  343.       to signal):
  344.  
  345.           sub is_tainted {
  346.           return ! eval    { join('',@_), kill 0; 1; };
  347.           }
  348.  
  349.       This is not -w clean,    however.  There    is no -w clean way to
  350.       detect taintedness - take this as a hint that    you should
  351.       untaint all possibly-tainted data.
  352.  
  353.       WWWWhhhhaaaatttt''''ssss aaaa cccclllloooossssuuuurrrreeee????
  354.  
  355.       Closures are documented in the _p_e_r_l_r_e_f manpage.
  356.  
  357.       _C_l_o_s_u_r_e is a computer    science    term with a precise but    hard-
  358.       to-explain meaning. Closures are implemented in Perl as
  359.       anonymous subroutines    with lasting references    to lexical
  360.       variables outside their own scopes.  These lexicals
  361.       magically refer to the variables that    were around when the
  362.       subroutine was defined (deep binding).
  363.  
  364.       Closures make    sense in any programming language where    you
  365.       can have the return value of a function be itself a
  366.       function, as you can in Perl.     Note that some    languages
  367.       provide anonymous functions but are not capable of providing
  368.       proper closures; the Python language,    for example.  For more
  369.       information on closures, check out any textbook on
  370.       functional programming.  Scheme is a language    that not only
  371.       supports but encourages closures.
  372.  
  373.       Here's a classic function-generating function:
  374.  
  375.           sub add_function_generator {
  376.         return sub { shift + shift };
  377.           }
  378.  
  379.           $add_sub = add_function_generator();
  380.           $sum = $add_sub->(4,5);             # $sum is 9 now.
  381.  
  382.       The closure works as a _f_u_n_c_t_i_o_n _t_e_m_p_l_a_t_e with    some
  383.       customization    slots left out to be filled later.  The
  384.       anonymous subroutine returned    by _a_d_d__f_u_n_c_t_i_o_n__g_e_n_e_r_a_t_o_r()
  385.       isn't    technically a closure because it refers    to no lexicals
  386.       outside its own scope.
  387.  
  388.       Contrast this    with the following _m_a_k_e__a_d_d_e_r()    function, in
  389.       which    the returned anonymous function    contains a reference
  390.  
  391.  
  392.  
  393.      Page 6                        (printed 10/23/98)
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.      PPPPEEEERRRRLLLLFFFFAAAAQQQQ7777((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLFFFFAAAAQQQQ7777((((1111))))
  401.  
  402.  
  403.  
  404.       to a lexical variable    outside    the scope of that function
  405.       itself.  Such    a reference requires that Perl return a    proper
  406.       closure, thus    locking    in for all time    the value that the
  407.       lexical had when the function    was created.
  408.  
  409.           sub make_adder {
  410.           my $addpiece = shift;
  411.           return sub { shift + $addpiece };
  412.           }
  413.  
  414.           $f1 = make_adder(20);
  415.           $f2 = make_adder(555);
  416.  
  417.       Now &$f1($n) is always 20 plus whatever $n you pass in,
  418.       whereas &$f2($n) is always 555 plus whatever $n you pass in.
  419.       The $addpiece    in the closure sticks around.
  420.  
  421.       Closures are often used for less esoteric purposes.  For
  422.       example, when    you want to pass in a bit of code into a
  423.       function:
  424.  
  425.           my $line;
  426.           timeout( 30, sub { $line = <STDIN> } );
  427.  
  428.       If the code to execute had been passed in as a string,
  429.       '$line = <STDIN>', there would have been no way for the
  430.       hypothetical _t_i_m_e_o_u_t() function to access the    lexical
  431.       variable $line back in its caller's scope.
  432.  
  433.       WWWWhhhhaaaatttt iiiissss vvvvaaaarrrriiiiaaaabbbblllleeee ssssuuuuiiiicccciiiiddddeeee aaaannnndddd hhhhoooowwww ccccaaaannnn IIII pppprrrreeeevvvveeeennnntttt iiiitttt????
  434.  
  435.       Variable suicide is when you (temporarily or permanently)
  436.       lose the value of a variable.     It is caused by scoping
  437.       through _m_y() and _l_o_c_a_l() interacting with either closures or
  438.       aliased _f_o_r_e_a_c_h() interator variables    and subroutine
  439.       arguments.  It used to be easy to inadvertently lose a
  440.       variable's value this    way, but now it's much harder.    Take
  441.       this code:
  442.  
  443.           my $f = "foo";
  444.           sub T {
  445.         while ($i++ < 3) { my $f = $f; $f .= "bar"; print $f, "\n" }
  446.           }
  447.           T;
  448.           print "Finally $f\n";
  449.  
  450.       The $f that has "bar"    added to it three times    should be a
  451.       new $f (my $f    should create a    new local variable each    time
  452.       through the loop).  It isn't,    however.  This is a bug, and
  453.       will be fixed.
  454.  
  455.  
  456.  
  457.  
  458.  
  459.      Page 7                        (printed 10/23/98)
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466.      PPPPEEEERRRRLLLLFFFFAAAAQQQQ7777((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLFFFFAAAAQQQQ7777((((1111))))
  467.  
  468.  
  469.  
  470.       HHHHoooowwww ccccaaaannnn IIII ppppaaaassssssss////rrrreeeettttuuuurrrrnnnn    aaaa {{{{FFFFuuuunnnnccccttttiiiioooonnnn,,,, FFFFiiiilllleeeeHHHHaaaannnnddddlllleeee,,,, AAAArrrrrrrraaaayyyy,,,,    HHHHaaaasssshhhh,,,,
  471.       MMMMeeeetttthhhhoooodddd,,,, RRRReeeeggggeeeexxxxpppp}}}}????
  472.  
  473.       With the exception of    regexps, you need to pass references
  474.       to these objects.  See the section on    _P_a_s_s _b_y    _R_e_f_e_r_e_n_c_e in
  475.       the _p_e_r_l_s_u_b manpage for this particular question, and    the
  476.       _p_e_r_l_r_e_f manpage for information on references.
  477.  
  478.       Passing Variables and    Functions
  479.           Regular variables    and functions are quite    easy: just
  480.           pass in a    reference to an    existing or anonymous variable
  481.           or function:
  482.  
  483.           func(    \$some_scalar );
  484.  
  485.           func(    \$some_array  );
  486.           func(    [ 1 .. 10 ]   );
  487.  
  488.           func(    \%some_hash   );
  489.           func(    { this => 10, that => 20 }   );
  490.  
  491.           func(    \&some_func   );
  492.           func(    sub { $_[0] ** $_[1] }     );
  493.  
  494.  
  495.       Passing Filehandles
  496.           To pass filehandles to subroutines, use the *FH or \*FH
  497.           notations.  These    are "typeglobs"    - see the section on
  498.           _T_y_p_e_g_l_o_b_s    _a_n_d _F_i_l_e_h_a_n_d_l_e_s    in the _p_e_r_l_d_a_t_a    manpage    and
  499.           especially the section on    _P_a_s_s _b_y    _R_e_f_e_r_e_n_c_e in the
  500.           _p_e_r_l_s_u_b manpage for more information.
  501.  
  502.           Here's an    excerpt:
  503.  
  504.           If you're    passing    around filehandles, you    could usually
  505.           just use the bare    typeglob, like *STDOUT,    but typeglobs
  506.           references would be better because they'll still work
  507.           properly under use strict    'refs'.     For example:
  508.  
  509.           splutter(\*STDOUT);
  510.           sub splutter {
  511.               my $fh = shift;
  512.               print $fh    "her um    well a hmmm\n";
  513.           }
  514.  
  515.           $rec = get_rec(\*STDIN);
  516.           sub get_rec {
  517.               my $fh = shift;
  518.               return scalar <$fh>;
  519.           }
  520.  
  521.           If you're    planning on generating new filehandles,    you
  522.  
  523.  
  524.  
  525.      Page 8                        (printed 10/23/98)
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532.      PPPPEEEERRRRLLLLFFFFAAAAQQQQ7777((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLFFFFAAAAQQQQ7777((((1111))))
  533.  
  534.  
  535.  
  536.           could do this:
  537.  
  538.           sub openit {
  539.               my $name = shift;
  540.               local *FH;
  541.               return open (FH, $path) ?    *FH : undef;
  542.           }
  543.           $fh =    openit('< /etc/motd');
  544.           print    <$fh>;
  545.  
  546.  
  547.       Passing Regexps
  548.           To pass regexps around, you'll need to either use    one of
  549.           the highly experimental regular expression modules from
  550.           CPAN (Nick Ing-Simmons's Regexp or Ilya Zakharevich's
  551.           Devel::Regexp), pass around strings and use an
  552.           exception-trapping eval, or else be be very, very
  553.           clever.  Here's an example of how    to pass    in a string to
  554.           be regexp    compared:
  555.  
  556.           sub compare($$) {
  557.               my ($val1, $regexp) = @_;
  558.               my $retval = eval    { $val =~ /$regexp/ };
  559.               die if $@;
  560.               return $retval;
  561.           }
  562.  
  563.           $match = compare("old    McDonald", q/d.*D/);
  564.  
  565.           Make sure    you never say something    like this:
  566.  
  567.           return eval "\$val =~    /$regexp/";   #    WRONG
  568.  
  569.           or someone can sneak shell escapes into the regexp due
  570.           to the double interpolation of the eval and the double-
  571.           quoted string.  For example:
  572.  
  573.           $pattern_of_evil = 'danger ${    system("rm -rf * &") } danger';
  574.  
  575.           eval "\$string =~ /$pattern_of_evil/";
  576.  
  577.           Those preferring to be very, very    clever might see the
  578.           O'Reilly book, _M_a_s_t_e_r_i_n_g _R_e_g_u_l_a_r _E_x_p_r_e_s_s_i_o_n_s, by Jeffrey
  579.           Friedl.  Page 273's _B_u_i_l_d__M_a_t_c_h_M_a_n_y__F_u_n_c_t_i_o_n() is
  580.           particularly interesting.     A complete citation of    this
  581.           book is given in the _p_e_r_l_f_a_q_2 manpage.
  582.  
  583.       Passing Methods
  584.           To pass an object    method into a subroutine, you can do
  585.           this:
  586.  
  587.  
  588.  
  589.  
  590.  
  591.      Page 9                        (printed 10/23/98)
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598.      PPPPEEEERRRRLLLLFFFFAAAAQQQQ7777((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLFFFFAAAAQQQQ7777((((1111))))
  599.  
  600.  
  601.  
  602.           call_a_lot(10, $some_obj, "methname")
  603.           sub call_a_lot {
  604.               my ($count, $widget, $trick) = @_;
  605.               for (my $i = 0; $i < $count; $i++) {
  606.               $widget->$trick();
  607.               }
  608.           }
  609.  
  610.           Or you can use a closure to bundle up the    object and its
  611.           method call and arguments:
  612.  
  613.           my $whatnot =     sub { $some_obj->obfuscate(@args) };
  614.           func($whatnot);
  615.           sub func {
  616.               my $code = shift;
  617.               &$code();
  618.           }
  619.  
  620.           You could    also investigate the _c_a_n() method in the
  621.           UNIVERSAL    class (part of the standard perl
  622.           distribution).
  623.  
  624.       HHHHoooowwww ddddoooo IIII ccccrrrreeeeaaaatttteeee aaaa ssssttttaaaattttiiiicccc vvvvaaaarrrriiiiaaaabbbblllleeee????
  625.  
  626.       As with most things in Perl, TMTOWTDI.  What is a "static
  627.       variable" in other languages could be    either a function-
  628.       private variable (visible only within    a single function,
  629.       retaining its    value between calls to that function), or a
  630.       file-private variable    (visible only to functions within the
  631.       file it was declared in) in Perl.
  632.  
  633.       Here's code to implement a function-private variable:
  634.  
  635.           BEGIN {
  636.           my $counter =    42;
  637.           sub prev_counter { return --$counter }
  638.           sub next_counter { return $counter++ }
  639.           }
  640.  
  641.       Now _p_r_e_v__c_o_u_n_t_e_r() and _n_e_x_t__c_o_u_n_t_e_r()    share a    private
  642.       variable $counter that was initialized at compile time.
  643.  
  644.       To declare a file-private variable, you'll still use a _m_y(),
  645.       putting it at    the outer scope    level at the top of the    file.
  646.       Assume this is in file Pax.pm:
  647.  
  648.           package Pax;
  649.           my $started = scalar(localtime(time()));
  650.  
  651.           sub begun    { return $started }
  652.  
  653.       When use Pax or require Pax loads this module, the variable
  654.  
  655.  
  656.  
  657.      Page 10                        (printed 10/23/98)
  658.  
  659.  
  660.  
  661.  
  662.  
  663.  
  664.      PPPPEEEERRRRLLLLFFFFAAAAQQQQ7777((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLFFFFAAAAQQQQ7777((((1111))))
  665.  
  666.  
  667.  
  668.       will be initialized.    It won't get garbage-collected the way
  669.       most variables going out of scope do,    because    the _b_e_g_u_n()
  670.       function cares about it, but no one else can get it.    It is
  671.       not called $Pax::started because its scope is    unrelated to
  672.       the package.    It's scoped to the file.  You could
  673.       conceivably have several packages in that same file all
  674.       accessing the    same private variable, but another file    with
  675.       the same package couldn't get    to it.
  676.  
  677.       See the section on _P_e_r_i_s_t_e_n_t _P_r_i_v_a_t_e _V_a_r_i_a_b_l_e_s in the
  678.       _p_e_r_l_s_u_b manpage for details.
  679.  
  680.       WWWWhhhhaaaatttt''''ssss tttthhhheeee ddddiiiiffffffffeeeerrrreeeennnncccceeee    bbbbeeeettttwwwweeeeeeeennnn    ddddyyyynnnnaaaammmmiiiicccc    aaaannnndddd lllleeeexxxxiiiiccccaaaallll ((((ssssttttaaaattttiiiicccc))))
  681.       ssssccccooooppppiiiinnnngggg????  BBBBeeeettttwwwweeeeeeeennnn _l_o_c_a_l() and    _m_y()?
  682.  
  683.       local($x) saves away the old value of    the global variable
  684.       $x, and assigns a new    value for the duration of the
  685.       subroutine, _w_h_i_c_h _i_s _v_i_s_i_b_l_e _i_n _o_t_h_e_r    _f_u_n_c_t_i_o_n_s _c_a_l_l_e_d _f_r_o_m
  686.       _t_h_a_t _s_u_b_r_o_u_t_i_n_e.  This is done at run-time, so is called
  687.       dynamic scoping.  _l_o_c_a_l() always affects global variables,
  688.       also called package variables    or dynamic variables.
  689.  
  690.       my($x) creates a new variable    that is    only visible in    the
  691.       current subroutine.  This is done at compile-time, so    is
  692.       called lexical or static scoping.  _m_y() always affects
  693.       private variables, also called lexical variables or
  694.       (improperly) _s_t_a_t_i_c(ly scoped) variables.
  695.  
  696.       For instance:
  697.  
  698.           sub visible {
  699.           print    "var has value $var\n";
  700.           }
  701.  
  702.           sub dynamic {
  703.           local    $var = 'local';      # new    temporary value    for the    still-global
  704.           visible();          #   variable called $var
  705.           }
  706.  
  707.           sub lexical {
  708.           my $var = 'private';      # new    private    variable, $var
  709.           visible();          # (invisible outside of sub scope)
  710.           }
  711.  
  712.           $var = 'global';
  713.  
  714.           visible();          # prints global
  715.           dynamic();          # prints local
  716.           lexical();          # prints global
  717.  
  718.       Notice how at    no point does the value    "private" get printed.
  719.       That's because $var only has that value within the block of
  720.  
  721.  
  722.  
  723.      Page 11                        (printed 10/23/98)
  724.  
  725.  
  726.  
  727.  
  728.  
  729.  
  730.      PPPPEEEERRRRLLLLFFFFAAAAQQQQ7777((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLFFFFAAAAQQQQ7777((((1111))))
  731.  
  732.  
  733.  
  734.       the _l_e_x_i_c_a_l()    function, and it is hidden from    called
  735.       subroutine.
  736.  
  737.       In summary, _l_o_c_a_l() doesn't make what    you think of as
  738.       private, local variables.  It    gives a    global variable    a
  739.       temporary value.  _m_y() is what you're    looking    for if you
  740.       want private variables.
  741.  
  742.       See the section on _P_r_i_v_a_t_e _V_a_r_i_a_b_l_e_s _v_i_a _m_y()    in the _p_e_r_l_s_u_b
  743.       manpage and the section on _T_e_m_p_o_r_a_r_y _V_a_l_u_e_s _v_i_a _l_o_c_a_l() in
  744.       the _p_e_r_l_s_u_b manpage for excruciating details.
  745.  
  746.       HHHHoooowwww ccccaaaannnn IIII aaaacccccccceeeessssssss aaaa ddddyyyynnnnaaaammmmiiiicccc vvvvaaaarrrriiiiaaaabbbblllleeee wwwwhhhhiiiilllleeee aaaa ssssiiiimmmmiiiillllaaaarrrrllllyyyy    nnnnaaaammmmeeeedddd
  747.       lllleeeexxxxiiiiccccaaaallll iiiissss iiiinnnn    ssssccccooooppppeeee????
  748.  
  749.       You can do this via symbolic references, provided you
  750.       haven't set use strict "refs".  So instead of    $var, use
  751.       ${'var'}.
  752.  
  753.           local $var = "global";
  754.           my    $var = "lexical";
  755.  
  756.           print "lexical is    $var\n";
  757.  
  758.           no strict    'refs';
  759.           print "global  is    ${'var'}\n";
  760.  
  761.       If you know your package, you    can just mention it
  762.       explicitly, as in $Some_Pack::var.  Note that    the notation
  763.       $::var is _n_o_t    the dynamic $var in the    current    package, but
  764.       rather the one in the    main package, as though    you had
  765.       written $main::var.  Specifying the package directly makes
  766.       you hard-code    its name, but it executes faster and avoids
  767.       running afoul    of use strict "refs".
  768.  
  769.       WWWWhhhhaaaatttt''''ssss tttthhhheeee ddddiiiiffffffffeeeerrrreeeennnncccceeee    bbbbeeeettttwwwweeeeeeeennnn    ddddeeeeeeeepppp aaaannnndddd sssshhhhaaaalllllllloooowwww bbbbiiiinnnnddddiiiinnnngggg????
  770.  
  771.       In deep binding, lexical variables mentioned in anonymous
  772.       subroutines are the same ones    that were in scope when    the
  773.       subroutine was created.  In shallow binding, they are
  774.       whichever variables with the same names happen to be in
  775.       scope    when the subroutine is called.    Perl always uses deep
  776.       binding of lexical variables (i.e., those created with
  777.       _m_y()).  However, dynamic variables (aka global, local, or
  778.       package variables) are effectively shallowly bound.
  779.       Consider this    just one more reason not to use    them.  See the
  780.       answer to the    section    on _W_h_a_t'_s _a _c_l_o_s_u_r_e?.
  781.  
  782.       WWWWhhhhyyyy ddddooooeeeessssnnnn''''tttt """"_m_y($foo)    = <FILE>;" work    right?
  783.  
  784.       my() and local() give    list context to    the right hand side of
  785.       =.  The <FH> read operation, like so many of Perl's
  786.  
  787.  
  788.  
  789.      Page 12                        (printed 10/23/98)
  790.  
  791.  
  792.  
  793.  
  794.  
  795.  
  796.      PPPPEEEERRRRLLLLFFFFAAAAQQQQ7777((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLFFFFAAAAQQQQ7777((((1111))))
  797.  
  798.  
  799.  
  800.       functions and    operators, can tell which context it was
  801.       called in and    behaves    appropriately.    In general, the
  802.       _s_c_a_l_a_r() function can    help.  This function does nothing to
  803.       the data itself (contrary to popular myth) but rather    tells
  804.       its argument to behave in whatever its scalar    fashion    is.
  805.       If that function doesn't have    a defined scalar behavior,
  806.       this of course doesn't help you (such    as with    _s_o_r_t()).
  807.  
  808.       To enforce scalar context in this particular case, however,
  809.       you need merely omit the parentheses:
  810.  
  811.           local($foo) = <FILE>;          #    WRONG
  812.           local($foo) = scalar(<FILE>);   #    ok
  813.           local $foo  = <FILE>;          #    right
  814.  
  815.       You should probably be using lexical variables anyway,
  816.       although the issue is    the same here:
  817.  
  818.           my($foo) = <FILE>;  # WRONG
  819.           my $foo  = <FILE>;  # right
  820.  
  821.  
  822.       HHHHoooowwww ddddoooo IIII rrrreeeeddddeeeeffffiiiinnnneeee aaaa bbbbuuuuiiiillllttttiiiinnnn ffffuuuunnnnccccttttiiiioooonnnn,,,,    ooooppppeeeerrrraaaattttoooorrrr,,,, oooorrrr mmmmeeeetttthhhhoooodddd????
  823.  
  824.       Why do you want to do    that? :-)
  825.  
  826.       If you want to override a predefined function, such as
  827.       _o_p_e_n(), then you'll have to import the new definition    from a
  828.       different module.  See the section on    _O_v_e_r_r_i_d_i_n_g _B_u_i_l_t_i_n
  829.       _F_u_n_c_t_i_o_n_s in the _p_e_r_l_s_u_b manpage.  There's also an example
  830.       in the section on _C_l_a_s_s::_T_e_m_p_l_a_t_e in the _p_e_r_l_t_o_o_t manpage.
  831.  
  832.       If you want to overload a Perl operator, such    as + or    **,
  833.       then you'll want to use the use overload pragma, documented
  834.       in the _o_v_e_r_l_o_a_d manpage.
  835.  
  836.       If you're talking about obscuring method calls in parent
  837.       classes, see the section on _O_v_e_r_r_i_d_d_e_n _M_e_t_h_o_d_s in the
  838.       _p_e_r_l_t_o_o_t manpage.
  839.  
  840.       WWWWhhhhaaaatttt''''ssss tttthhhheeee ddddiiiiffffffffeeeerrrreeeennnncccceeee    bbbbeeeettttwwwweeeeeeeennnn    ccccaaaalllllllliiiinnnngggg    aaaa ffffuuuunnnnccccttttiiiioooonnnn aaaassss &&&&ffffoooooooo aaaannnndddd
  841.       _f_o_o()?
  842.  
  843.       When you call    a function as &foo, you    allow that function
  844.       access to your current @_ values, and    you by-pass
  845.       prototypes.  That means that the function doesn't get    an
  846.       empty    @_, it gets yours!  While not strictly speaking    a bug
  847.       (it's    documented that    way in the _p_e_r_l_s_u_b manpage), it    would
  848.       be hard to consider this a feature in    most cases.
  849.  
  850.       When you call    your function as &foo(), then you _d_o get a new
  851.       @_, but prototyping is still circumvented.
  852.  
  853.  
  854.  
  855.      Page 13                        (printed 10/23/98)
  856.  
  857.  
  858.  
  859.  
  860.  
  861.  
  862.      PPPPEEEERRRRLLLLFFFFAAAAQQQQ7777((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLFFFFAAAAQQQQ7777((((1111))))
  863.  
  864.  
  865.  
  866.       Normally, you    want to    call a function    using foo().  You may
  867.       only omit the    parentheses if the function is already known
  868.       to the compiler because it already saw the definition    (use
  869.       but not require), or via a forward reference or use subs
  870.       declaration.    Even in    this case, you get a clean @_ without
  871.       any of the old values    leaking    through    where they don't
  872.       belong.
  873.  
  874.       HHHHoooowwww ddddoooo IIII ccccrrrreeeeaaaatttteeee aaaa sssswwwwiiiittttcccchhhh oooorrrr ccccaaaasssseeee ssssttttaaaatttteeeemmmmeeeennnntttt????
  875.  
  876.       This is explained in more depth in the the _p_e_r_l_s_y_n manpage.
  877.       Briefly, there's no official case statement, because of the
  878.       variety of tests possible in Perl (numeric comparison,
  879.       string comparison, glob comparison, regexp matching,
  880.       overloaded comparisons, ...).     Larry couldn't    decide how
  881.       best to do this, so he left it out, even though it's been on
  882.       the wish list    since perl1.
  883.  
  884.       The general answer is    to write a construct like this:
  885.  
  886.           for ($variable_to_test) {
  887.           if    (/pat1/)  { }      # do something
  888.           elsif    (/pat2/)  { }      # do something else
  889.           elsif    (/pat3/)  { }      # do something else
  890.           else          { }      # default
  891.           }
  892.  
  893.       Here's a simple example of a switch based on pattern
  894.       matching, this time lined up in a way    to make    it look    more
  895.       like a switch    statement.  We'll do a multi-way conditional
  896.       based    on the type of reference stored    in $whatchamacallit:
  897.  
  898.           SWITCH: for (ref $whatchamacallit) {
  899.  
  900.           /^$/          && die "not a    reference";
  901.  
  902.           /SCALAR/      && do    {
  903.                       print_scalar($$ref);
  904.                       last SWITCH;
  905.                   };
  906.  
  907.           /ARRAY/      && do    {
  908.                       print_array(@$ref);
  909.                       last SWITCH;
  910.                   };
  911.  
  912.           /HASH/      && do    {
  913.                       print_hash(%$ref);
  914.                       last SWITCH;
  915.                   };
  916.  
  917.  
  918.  
  919.  
  920.  
  921.      Page 14                        (printed 10/23/98)
  922.  
  923.  
  924.  
  925.  
  926.  
  927.  
  928.      PPPPEEEERRRRLLLLFFFFAAAAQQQQ7777((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLFFFFAAAAQQQQ7777((((1111))))
  929.  
  930.  
  931.  
  932.           /CODE/      && do    {
  933.                       warn "can't print function ref";
  934.                       last SWITCH;
  935.                   };
  936.  
  937.           # DEFAULT
  938.  
  939.           warn "User defined type skipped";
  940.  
  941.           }
  942.  
  943.       See perlsyn/"Basic BLOCKs and    Switch Statements" for many
  944.       other    examples in this style.
  945.  
  946.       Sometimes you    should change the positions of the constant
  947.       and the variable.  For example, let's    say you    wanted to test
  948.       which    of many    answers    you were given,    but in a case-
  949.       insensitive way that also allows abbreviations.  You can use
  950.       the following    technique if the strings all start with
  951.       different characters,    or if you want to arrange the matches
  952.       so that one takes precedence over another, as    "SEND" has
  953.       precedence over "STOP" here:
  954.  
  955.           chomp($answer = <>);
  956.           if    ("SEND"  =~    /^\Q$answer/i) { print "Action is send\n"  }
  957.           elsif ("STOP"  =~    /^\Q$answer/i) { print "Action is stop\n"  }
  958.           elsif ("ABORT" =~    /^\Q$answer/i) { print "Action is abort\n" }
  959.           elsif ("LIST"  =~    /^\Q$answer/i) { print "Action is list\n"  }
  960.           elsif ("EDIT"  =~    /^\Q$answer/i) { print "Action is edit\n"  }
  961.  
  962.       A totally different approach is to create a hash of function
  963.       references.
  964.  
  965.           my %commands = (
  966.           "happy" => \&joy,
  967.           "sad",  => \&sullen,
  968.           "done"  => sub { die "See ya!" },
  969.           "mad"      => \&angry,
  970.           );
  971.  
  972.           print "How are you? ";
  973.           chomp($string = <STDIN>);
  974.           if ($commands{$string}) {
  975.           $commands{$string}->();
  976.           }    else {
  977.           print    "No such command: $string\n";
  978.           }
  979.  
  980.  
  981.  
  982.  
  983.  
  984.  
  985.  
  986.  
  987.      Page 15                        (printed 10/23/98)
  988.  
  989.  
  990.  
  991.  
  992.  
  993.  
  994.      PPPPEEEERRRRLLLLFFFFAAAAQQQQ7777((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLFFFFAAAAQQQQ7777((((1111))))
  995.  
  996.  
  997.  
  998.       HHHHoooowwww ccccaaaannnn IIII ccccaaaattttcccchhhh aaaacccccccceeeesssssssseeeessss ttttoooo uuuunnnnddddeeeeffffiiiinnnneeeedddd
  999.       vvvvaaaarrrriiiiaaaabbbblllleeeessss////ffffuuuunnnnccccttttiiiioooonnnnssss////mmmmeeeetttthhhhooooddddssss????
  1000.  
  1001.       The AUTOLOAD method, discussed in the    section    on _A_u_t_o_l_o_a_d_i_n_g
  1002.       in the _p_e_r_l_s_u_b manpage and the section on _A_U_T_O_L_O_A_D: _P_r_o_x_y
  1003.       _M_e_t_h_o_d_s in the _p_e_r_l_t_o_o_t manpage, lets    you capture calls to
  1004.       undefined functions and methods.
  1005.  
  1006.       When it comes    to undefined variables that would trigger a
  1007.       warning under    -w, you    can use    a handler to trap the pseudo-
  1008.       signal __WARN__ like this:
  1009.  
  1010.           $SIG{__WARN__} = sub {
  1011.  
  1012.           for (    $_[0] )    {      # voici un switch statement
  1013.  
  1014.               /Use of uninitialized value/  && do {
  1015.               # promote warning to a fatal
  1016.               die $_;
  1017.               };
  1018.  
  1019.               #    other warning cases to catch could go here;
  1020.  
  1021.               warn $_;
  1022.           }
  1023.  
  1024.           };
  1025.  
  1026.  
  1027.       WWWWhhhhyyyy ccccaaaannnn''''tttt aaaa mmmmeeeetttthhhhoooodddd iiiinnnncccclllluuuuddddeeeedddd iiiinnnn tttthhhhiiiissss ssssaaaammmmeeee ffffiiiilllleeee    bbbbeeee ffffoooouuuunnnndddd????
  1028.  
  1029.       Some possible    reasons: your inheritance is getting confused,
  1030.       you've misspelled the    method name, or    the object is of the
  1031.       wrong    type.  Check out the _p_e_r_l_t_o_o_t manpage for details on
  1032.       these.  You may also use print ref($object) to find out the
  1033.       class    $object    was blessed into.
  1034.  
  1035.       Another possible reason for problems is because you've used
  1036.       the indirect object syntax (eg, find Guru "Samy") on a class
  1037.       name before Perl has seen that such a    package    exists.     It's
  1038.       wisest to make sure your packages are    all defined before you
  1039.       start    using them, which will be taken    care of    if you use the
  1040.       use statement    instead    of require.  If    not, make sure to use
  1041.       arrow    notation (eg, Guru->find("Samy")) instead.  Object
  1042.       notation is explained    in the _p_e_r_l_o_b_j manpage.
  1043.  
  1044.       Make sure to read about creating modules in the _p_e_r_l_m_o_d
  1045.       manpage and the perils of indirect objects in    the section on
  1046.       _W_A_R_N_I_N_G in the _p_e_r_l_o_b_j manpage.
  1047.  
  1048.  
  1049.  
  1050.  
  1051.  
  1052.  
  1053.      Page 16                        (printed 10/23/98)
  1054.  
  1055.  
  1056.  
  1057.  
  1058.  
  1059.  
  1060.      PPPPEEEERRRRLLLLFFFFAAAAQQQQ7777((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLFFFFAAAAQQQQ7777((((1111))))
  1061.  
  1062.  
  1063.  
  1064.       HHHHoooowwww ccccaaaannnn IIII ffffiiiinnnndddd oooouuuutttt mmmmyyyy    ccccuuuurrrrrrrreeeennnntttt    ppppaaaacccckkkkaaaaggggeeee????
  1065.  
  1066.       If you're just a random program, you can do this to find out
  1067.       what the currently compiled package is:
  1068.  
  1069.           my $packname = __PACKAGE__;
  1070.  
  1071.       But if you're    a method and you want to print an error
  1072.       message that includes    the kind of object you were called on
  1073.       (which is not    necessarily the    same as    the one    in which you
  1074.       were compiled):
  1075.  
  1076.           sub amethod {
  1077.           my $self  = shift;
  1078.           my $class = ref($self) || $self;
  1079.           warn "called me from a $class    object";
  1080.           }
  1081.  
  1082.  
  1083.       HHHHoooowwww ccccaaaannnn IIII ccccoooommmmmmmmeeeennnntttt oooouuuutttt    aaaa llllaaaarrrrggggeeee    bbbblllloooocccckkkk ooooffff ppppeeeerrrrllll ccccooooddddeeee????
  1084.  
  1085.       Use embedded POD to discard it:
  1086.  
  1087.           #    program    is here
  1088.  
  1089.           =for nobody
  1090.           This paragraph is    commented out
  1091.  
  1092.           #    program    continues
  1093.  
  1094.           =begin comment text
  1095.  
  1096.           all of this stuff
  1097.  
  1098.           here will    be ignored
  1099.           by everyone
  1100.  
  1101.           =end comment text
  1102.  
  1103.           =cut
  1104.  
  1105.       This can't go    just anywhere.    You have to put    a pod
  1106.       directive where the parser is    expecting a new    statement, not
  1107.       just in the middle of    an expression or some other arbitrary
  1108.       yacc grammar production.
  1109.  
  1110.      AAAAUUUUTTTTHHHHOOOORRRR AAAANNNNDDDD    CCCCOOOOPPPPYYYYRRRRIIIIGGGGHHHHTTTT
  1111.       Copyright (c)    1997, 1998 Tom Christiansen and    Nathan
  1112.       Torkington.  All rights reserved.
  1113.  
  1114.       When included    as part    of the Standard    Version    of Perl, or as
  1115.       part of its complete documentation whether printed or
  1116.  
  1117.  
  1118.  
  1119.      Page 17                        (printed 10/23/98)
  1120.  
  1121.  
  1122.  
  1123.  
  1124.  
  1125.  
  1126.      PPPPEEEERRRRLLLLFFFFAAAAQQQQ7777((((1111))))     22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLFFFFAAAAQQQQ7777((((1111))))
  1127.  
  1128.  
  1129.  
  1130.       otherwise, this work may be distributed only under the terms
  1131.       of Perl's Artistic License.  Any distribution    of this    file
  1132.       or derivatives thereof _o_u_t_s_i_d_e of that package require that
  1133.       special arrangements be made with copyright holder.
  1134.  
  1135.       Irrespective of its distribution, all    code examples in this
  1136.       file are hereby placed into the public domain.  You are
  1137.       permitted and    encouraged to use this code in your own
  1138.       programs for fun or for profit as you    see fit.  A simple
  1139.       comment in the code giving credit would be courteous but is
  1140.       not required.
  1141.  
  1142.  
  1143.  
  1144.  
  1145.  
  1146.  
  1147.  
  1148.  
  1149.  
  1150.  
  1151.  
  1152.  
  1153.  
  1154.  
  1155.  
  1156.  
  1157.  
  1158.  
  1159.  
  1160.  
  1161.  
  1162.  
  1163.  
  1164.  
  1165.  
  1166.  
  1167.  
  1168.  
  1169.  
  1170.  
  1171.  
  1172.  
  1173.  
  1174.  
  1175.  
  1176.  
  1177.  
  1178.  
  1179.  
  1180.  
  1181.  
  1182.  
  1183.  
  1184.  
  1185.      Page 18                        (printed 10/23/98)
  1186.  
  1187.  
  1188.  
  1189.